package com.fzrskj.cloud.free.dic.utils;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.utils.DoubleUtil;
import com.fzrskj.cloud.free.dic.bean.DicVipgrade;
import com.fzrskj.cloud.free.dic.dao.DicVipgradeDao;
import com.fzrskj.cloud.free.dic.utils.vo.CloudAgentPriceVo;
import com.fzrskj.cloud.free.sys.bean.SysProductView;
import com.fzrskj.cloud.free.sys.utils.SysProductUtils;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.dao.UserAccountDao;
import ms.core.tool.StrTool;

/**
 * CloudPriceUtils.
 *
 * @author zj.
 *         Created on 2019/8/23 0023.
 */
public class CloudAgentPriceUtils {
    /**
     * 计算云产品代理价格.
     * @param productCode 产品编号.
     * @param buyOperation 操作类型.
     * @param sourceTotalPrice 总价.
     * @param sourcePayPrice 售价.
     * @param agentId 代理ID.
     * @return 计算结果.
     */
    public static CloudAgentPriceVo computeAgentPrice(String productCode, Integer buyOperation, Double sourceTotalPrice,
                                                      Double sourcePayPrice, Integer agentId) {
        return computeAgentPriceCommon(null, ProductTypeEnum.CLOUD.getTag(), productCode, buyOperation,
                sourceTotalPrice, sourcePayPrice, agentId);
    }

    /**
     * 计算虚拟主机产品代理价格.
     * @param productCode 产品编号.
     * @param buyOperation 操作类型.
     * @param sourceTotalPrice 总价.
     * @param sourcePayPrice 售价.
     * @param agentId 代理ID.
     * @return 计算结果.
     */
    public static CloudAgentPriceVo computeAgentPriceVHost(String regType, String productCode, Integer buyOperation, Double sourceTotalPrice,
                                                           Double sourcePayPrice, Integer agentId) {
        return computeAgentPriceCommon(regType, ProductTypeEnum.V_HOST.getTag(), productCode, buyOperation,
                sourceTotalPrice, sourcePayPrice, agentId);
    }

    /**
     * 计算虚拟主机产品代理价格.
     * @param productCode 产品编号.
     * @param buyOperation 操作类型.
     * @param sourceTotalPrice 总价.
     * @param sourcePayPrice 售价.
     * @param agentId 代理ID.
     * @return 计算结果.
     */
    public static CloudAgentPriceVo computeAgentPriceDiy(String productCode, Integer buyOperation, Double sourceTotalPrice,
                                                           Double sourcePayPrice, Integer agentId, Boolean firstDiscount) {
        return computeAgentPriceCommon(ProductTypeEnum.DIY.getTag(), ProductTypeEnum.DIY.getTag(), productCode, buyOperation,
                sourceTotalPrice, sourcePayPrice, agentId, firstDiscount);
    }

    public static CloudAgentPriceVo computeAgentPriceCommon(String regType, String productClass, String productCode, Integer buyOperation, Double sourceTotalPrice,
                                                            Double sourcePayPrice, Integer agentId) {
        return computeAgentPriceCommon(regType, productClass, productCode, buyOperation, sourceTotalPrice, sourcePayPrice, agentId, true);
    }

    /**
     * 按溢价率计算云产品代理价格.
     * @param productClass 产品类型.
     * @param productCode 产品编号.
     * @param buyOperation 操作类型.
     * @param sourceTotalPrice 总价.
     * @param sourcePayPrice 售价.
     * @param agentId 代理ID.
     * @return 计算结果.
     */
    public static CloudAgentPriceVo computeAgentPriceCommon(String regType, String productClass, String productCode, Integer buyOperation, Double sourceTotalPrice,
                                                      Double sourcePayPrice, Integer agentId, Boolean firstDiscount) {
        CloudAgentPriceVo priceVo = new CloudAgentPriceVo();
        if (agentId < 1) {
            priceVo.setTotalPrice(sourceTotalPrice);
            priceVo.setPayPrice(sourcePayPrice);
            priceVo.setMessage("已是顶级平台价格");
            priceVo.setComputeResult(true);
            return priceVo;
        }

        SysProductUtils productUtils = new SysProductUtils();
        SysProductView product = null;
        if (StrTool.isBlankStr(regType)) {
            product = productUtils.querySysProductAppointAgent(productClass, productCode, agentId);
        } else {
            product = productUtils.querySysProductAppointAgent(productClass, productCode, regType, agentId);
        }
        if (null == product) {
            return priceVo;
        }
        JSONObject params = JSONObject.parseObject(product.getProductParam());
        if (null == params) {
            return priceVo;
        }

        Double premium = 0.0;
        if (1 == buyOperation) {
            premium = params.containsKey("premium") ? params.getDouble("premium") : premium;
        } else if (2 == buyOperation) {
            premium = params.containsKey("premiumRenew") ? params.getDouble("premiumRenew") : premium;
        } else if (3 == buyOperation) {
            premium = params.containsKey("premiumUpgrade") ? params.getDouble("premiumUpgrade") : premium;
        } else {
            priceVo.setMessage("不支持的操作类型");
            return priceVo;
        }

        Double premiumResult = 1 + DoubleUtil.divide(premium, 100.0);
        Double totalPrice = DoubleUtil.mul(sourceTotalPrice, premiumResult);
        Double payPrice = DoubleUtil.mul(sourcePayPrice, premiumResult);

        UserAccountDao accountDao = new UserAccountDao();
        UserAccount account = accountDao.getUserAccountWithoutAgent(agentId);

        //计算代理等级打折优惠
        //会员等级价格
        if (firstDiscount) {
            DicVipgradeDao vipgradeDao = new DicVipgradeDao();
            DicVipgrade ua = vipgradeDao.queryDicVipgradeByUidAppointAgent(agentId, account.getAgentId());
            if (ua != null) {
                if (ua.getRebate() > 0) {
                    //打折只影响实际售价，可抬高市场价.
                    payPrice = DoubleUtil.mul(payPrice, ua.getRebate() / 10);
                }
            }
        }


        //是否有上级代理，有继续计算
        if (account.getAgentId() > 0) {
            return computeAgentPriceCommon(regType, productClass, productCode, buyOperation, totalPrice, payPrice, account.getAgentId());
        }

        priceVo.setTotalPrice(totalPrice);
        priceVo.setPayPrice(payPrice);
        priceVo.setComputeResult(true);
        priceVo.setMessage("获取价格成功");
        return priceVo;
    }
}

